import logging
import sys
from typing import List

# from databases import DatabaseURL
from loguru import logger
from starlette.config import Config
from starlette.datastructures import CommaSeparatedStrings, Secret

from app.core.logging import InterceptHandler
from app.core.logging import setup_logger

API_PREFIX = "/audit_api"

JWT_TOKEN_PREFIX = "Token"  # noqa: S105
VERSION = "1.0"

config = Config(".env")

DEBUG: bool = config("DEBUG", cast=bool, default=False)
# cmdb配置
DB_CMDB_URL: str = config("DB_CMDB_URL", cast=str)
DB_CMDB_USER: str = config("DB_CMDB_USER", cast=str)
DB_CMDB_PASSWORD: str = config("DB_CMDB_PASSWORD", cast=str)

# 平台配置
DB_PLATFORM_URL: str = config("DB_PLATFORM_URL", cast=str)
DB_PLATFORM_USER: str = config("DB_PLATFORM_USER", cast=str)
DB_PLATFORM_PASSWORD: str = config("DB_PLATFORM_PASSWORD", cast=str)

# GO INCEPTION 配置
GOINCEPTION_HOST: str = config("GOINCEPTION_HOST", cast=str)
GOINCEPTION_PORT: int = config("GOINCEPTION_PORT", cast=int)
GOINCEPTION_USER: str = config("GOINCEPTION_USER", cast=str)
GOINCEPTION_PASSWORD: str = config("GOINCEPTION_PASSWORD", cast=str)
GOINCEPTION_CHARSET: str = config("GOINCEPTION_CHARSET", cast=str)

MAX_COLUMNS_VALUE: int = config("MAX_COLUMNS_VALUE", cast=int)
COLUMNS_WARNING_VALUE: int = config("COLUMNS_WARNING_VALUE", cast=int)
VARCHAR_MAX_LENGTH: int = config("VARCHAR_MAX_LENGTH", cast=int)

large_dml_exec_interval: float = config("large_dml_exec_interval", cast=float)
backup_fields_separate: str = config("backup_fields_separate", cast=str)

SQL_EXECUTE_USER: str = config("SQL_EXECUTE_USER", cast=str)

# 临时库配置
TMP_DATABASE_INSTANCE_HOST: str = config("TMP_DATABASE_INSTANCE_HOST", cast=str)
TMP_DATABASE_INSTANCE_PORT: int = config("TMP_DATABASE_INSTANCE_PORT", cast=int)
TMP_DATABASE_INSTANCE_USER: str = config("TMP_DATABASE_INSTANCE_USER", cast=str)
TMP_DATABASE_INSTANCE_PASSWORD: str = config("TMP_DATABASE_INSTANCE_PASSWORD", cast=str)

# 日志参数
log_file_path: str = config("log_file_path", cast=str)
log_file_name: str = config("log_file_name", cast=str)
logger_mode: str = config("logger_mode", cast=str)

is_clean_tmp_data: int = config("is_clean_tmp_data", cast=int)

SECRET_KEY: Secret = config("SECRET_KEY", cast=Secret)

PROJECT_NAME: str = config("PROJECT_NAME")
ALLOWED_HOSTS: List[str] = config(
    "ALLOWED_HOSTS",
    cast=CommaSeparatedStrings,
    default="",
)

dml_batch_tmp_file_path: str = config("dml_batch_tmp_file_path", cast=str)
mysql_backup_before_execute_url: str = config("mysql_backup_before_execute_url", cast=str)

# logging configuration
exec_log_file = f'{log_file_path}{log_file_name}'
setup_logger('exec_log', exec_log_file, level=logging.DEBUG, logger_mode=logger_mode)
exec_log = logging.getLogger('exec_log')
#
# LOGGING_LEVEL = logging.DEBUG if DEBUG else logging.INFO
# LOGGERS = ("uvicorn.asgi", "uvicorn.access")
#
# logging.getLogger().handlers = [InterceptHandler()]
# for logger_name in LOGGERS:
#     logging_logger = logging.getLogger(logger_name)
#     logging_logger.handlers = [InterceptHandler(level=LOGGING_LEVEL)]
#
# logger.configure(handlers=[{"sink": sys.stderr, "level": LOGGING_LEVEL}])
